package com.example.examsystem.controller;

import com.example.examsystem.dto.CategoryDto;
import com.example.examsystem.dto.PageResponse;
import com.example.examsystem.service.CategoryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/question-categories")
@RequiredArgsConstructor
public class QuestionCategoryController {

    private final CategoryService categoryService;

    /**
     * 获取分类列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('question:category:view')")
    public ResponseEntity<PageResponse<CategoryDto>> getCategories(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String code,
            @RequestParam(required = false) Long subjectId,
            @RequestParam(required = false) String status) {
        
        PageResponse<CategoryDto> categories = categoryService.getCategories(
            page, size, name, code, subjectId, status);
        return ResponseEntity.ok(categories);
    }

    /**
     * 获取激活的分类列表
     */
    @GetMapping("/active")
    public ResponseEntity<List<CategoryDto>> getActiveCategories(
            @RequestParam(required = false) Long subjectId) {
        
        List<CategoryDto> categories = subjectId != null 
            ? categoryService.getCategoriesBySubject(subjectId)
            : categoryService.getAllActiveCategories();
        return ResponseEntity.ok(categories);
    }

    /**
     * 获取分类详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('question:category:view')")
    public ResponseEntity<CategoryDto> getCategory(@PathVariable Long id) {
        CategoryDto category = categoryService.getCategoryById(id);
        return ResponseEntity.ok(category);
    }

    /**
     * 创建分类
     */
    @PostMapping
    @PreAuthorize("hasAuthority('question:category:create')")
    public ResponseEntity<CategoryDto> createCategory(@Valid @RequestBody CategoryDto categoryDto) {
        CategoryDto created = categoryService.createCategory(categoryDto);
        return ResponseEntity.ok(created);
    }

    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('question:category:update')")
    public ResponseEntity<CategoryDto> updateCategory(
            @PathVariable Long id, 
            @Valid @RequestBody CategoryDto categoryDto) {
        CategoryDto updated = categoryService.updateCategory(id, categoryDto);
        return ResponseEntity.ok(updated);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('question:category:delete')")
    public ResponseEntity<Void> deleteCategory(@PathVariable Long id) {
        categoryService.deleteCategory(id);
        return ResponseEntity.ok().build();
    }
}